package com.example.maysucceedapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Login extends AppCompatActivity {
    private EditText ed_username;
    private EditText ed_password;
    private SharedPreferences mSharedPreference;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ed_username=findViewById(R.id.ed_username);
        mSharedPreference=getSharedPreferences("user",MODE_PRIVATE);
        ed_password=findViewById(R.id.ed_password);

        //dianji注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册页面
                Intent intent=new Intent(Login.this, Register.class);
                startActivity(intent);
            }



        });
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username=ed_username.getText().toString();
                String password=ed_password.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
                    Toast.makeText(Login.this, "请输入", Toast.LENGTH_SHORT).show();
                }else{
                    String name=mSharedPreference.getString("username",null);
                    String password1=mSharedPreference.getString("password",null);
                    if(username.equals(name)&&password.equals(password1)){
                        Toast.makeText(Login.this, "成功", Toast.LENGTH_SHORT).show();
                        Intent i=new Intent(Login.this,MainActivity.class);
                        startActivity(i);

                    }else{
                        Toast.makeText(Login.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();

                    }

                }
            }
        });
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}